package com.hfdg.prototype.base.generator;

import com.hfdg.content.entity.Content;
import com.hfdg.organization.entity.Clerk;
import com.hfdg.prototype.base.annotations.Column;
import com.hfdg.prototype.base.annotations.Table;
import com.hfdg.prototype.base.utils.SqlBuilder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PageGenerator
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/21 20:11
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
public class PageGenerator {
    static String company="会飞的锅工作室";
    static String author="yuelh";
    static String email="18695867679@163.com";
    private static final Resource RESOURCE = new ClassPathResource("gen");
    private static Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
    private static File baseDir;
    static{
        baseDir = new File(System.getProperty("user.dir")+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"templates");
        System.out.println(baseDir.getAbsolutePath());
        if(!baseDir.exists()){
            baseDir.mkdirs();
        }
    }

    public static void buildCreateView(Class<?> clazz){
        System.out.println("clazz = [" + clazz + "]新增视图创建开始！");
        List<DataField> dataFieldList = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            DataField dataField = new DataField(field.getName(),field.getAnnotation(Column.class).title());
            dataFieldList.add(dataField);
        }
        try{
            configuration.setDirectoryForTemplateLoading(RESOURCE.getFile());
            File formPageFile = RESOURCE.getFile();
            Template formPageTemplate = configuration.getTemplate("createView.ftl");
            Map<String,Object> root = new HashMap<>();
            root.put("dataFieldList",dataFieldList);
            root.put("modelName", StringUtils.uncapitalize(clazz.getSimpleName()));
            root.put("pageName",clazz.getAnnotation(Table.class).title());
            root.put("company",company);
            root.put("author",author);
            root.put("email",email);
            File formHtmlFile = new File(baseDir,StringUtils.uncapitalize(clazz.getSimpleName())+"CreateView.html");
            if(!formHtmlFile.exists()){
                formHtmlFile.createNewFile();
            }
            formPageTemplate.process(root,new FileWriter(formHtmlFile));
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("clazz = [" + clazz + "]新增视图创建结束！");
    }
    public static void buildEditView(Class<?> clazz){
        System.out.println("clazz = [" + clazz + "]编辑视图创建开始！");
        List<DataField> dataFieldList = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            DataField dataField = new DataField(field.getName(),field.getAnnotation(Column.class).title());
            dataFieldList.add(dataField);
        }
        try{
            configuration.setDirectoryForTemplateLoading(RESOURCE.getFile());
            File formPageFile = RESOURCE.getFile();
            Template formPageTemplate = configuration.getTemplate("editView.ftl");
            Map<String,Object> root = new HashMap<>();
            root.put("dataFieldList",dataFieldList);
            root.put("modelName", StringUtils.uncapitalize(clazz.getSimpleName()));
            root.put("pageName",clazz.getAnnotation(Table.class).title());
            root.put("company",company);
            root.put("author",author);
            root.put("email",email);
            File formHtmlFile = new File(baseDir,StringUtils.uncapitalize(clazz.getSimpleName())+"EditView.html");
            if(!formHtmlFile.exists()){
                formHtmlFile.createNewFile();
            }
            formPageTemplate.process(root,new FileWriter(formHtmlFile));
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("clazz = [" + clazz + "]编辑视图创建结束！");
    }
    public static void buildListView(Class<?> clazz){
        System.out.println("clazz = [" + clazz + "]列表视图创建开始！");
        List<DataField> dataFieldList = new ArrayList<>();
        Field[] fields = FieldUtils.getAllFields(clazz);
        for(Field field:fields){
            DataField dataField = new DataField(field.getName(),field.getAnnotation(Column.class).title());
            dataFieldList.add(dataField);
        }
        try{
            configuration.setDirectoryForTemplateLoading(RESOURCE.getFile());
            File formPageFile = RESOURCE.getFile();
            Template listViewTemplate = configuration.getTemplate("listView.ftl");
            Map<String,Object> root = new HashMap<>();
            root.put("dataFieldList",dataFieldList);
            root.put("modelName", StringUtils.uncapitalize(clazz.getSimpleName()));
            root.put("pageName",clazz.getAnnotation(Table.class).title());
            root.put("company",company);
            root.put("author",author);
            root.put("email",email);
            File listViewFile = new File(baseDir,StringUtils.uncapitalize(clazz.getSimpleName())+"ListView.html");
            if(!listViewFile.exists()){
                listViewFile.createNewFile();
            }
            listViewTemplate.process(root,new FileWriter(listViewFile));
        }catch (Exception e){
        }
        System.out.println("clazz = [" + clazz + "]列表视图创建结束！");
    }
    public static void main(String[] args) {
//        List<String> clazzes = SqlBuilder.getClassName("com.hfdg.coordination.entity");
//        for(String clazz:clazzes){
//            try {
//                buildCreateView(Class.forName(clazz));
//                buildEditView(Class.forName(clazz));
//                buildListView(Class.forName(clazz));
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//
//        }
//        Class clazz = com.hfdg.filecenter.entity.File.class;
//        buildCreateView(clazz);
//        buildEditView(clazz);
//        buildListView(clazz);
    }
}
